package com.authine.cloudpivot.ext.dto.model.trade;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.Enum.SettleType;
import com.authine.cloudpivot.ext.Enum.TradeSettleStatus;
import com.authine.cloudpivot.ext.dto.Summary;
import com.authine.cloudpivot.ext.enums.order.OrderGoodsReturnsStatusEnum;
import com.authine.cloudpivot.ext.enums.order.OrderGoodsStatusEnum;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Builder
@Schema(description = "交易记录表")
public class Trade {

    @Schema(description = "ID")
    private String id;

    @Schema(description = "erp_po")
    private String erp_po;

    @Schema(description = "方向", required = true)
    private String direction;

    @Schema(description = "业务类型,枚举类 TradeOrderType", required = true)
    private String business_type;

    @Schema(description = "目标组织ID")
    private SelectorFormat organization_id;

    @Schema(description = "目标组织名称")
    private String organization_name;

    @Schema(description = "交易所属用户ID")
    private String member_id;

    @Schema(description = "交易所属用户名称（下单人）")
    private String member_name;

    @Schema(description = "订单商品ID", required = true)
    private String order_goods_id;

    @Schema(description = "相反交易类型的订单商品ID", required = true)
    private String target_order_goods_id;

    @Schema(description = "单价（含税）")
    private BigDecimal price;

    @Schema(description = "数量")
    private BigDecimal quantity;

    @Schema(description = "商品税率")
    private BigDecimal tax_rate;

    @Schema(description = "商品金额，数量x含税单价")
    private BigDecimal total;

    @Schema(description = "账期快照", required = true)
    private BigDecimal account_period;

    @Schema(description = "真实账期", required = true)
    private String actual_account_period;

    @Schema(description = "交易摘要")
    private Summary summary;

    @Schema(description = "供应商订单ID")
    private String vendor_order_id;

    @Schema(description = "客户订单ID")
    private String customer_order_id;

    @Schema(description = "运费金额")
    private BigDecimal freight;

    @Schema(description = "平台费率")
    private BigDecimal platform_rate;

    @Schema(description = "金融费率")
    private BigDecimal interest_rate;

    @Schema(description = "其它费率")
    private BigDecimal other_rate;

    @Schema(description = "是否为转采订单")
    private boolean transfer_purchase;

    @Schema(description = "金税编码")
    private String golden_tax;

    @Schema(description = "可申请开票数量")
    private BigDecimal allowed_invoice_quantity;

    @Schema(description = "可申请开票金额")
    private BigDecimal allowed_invoice_total;

    @Schema(description = "可结算金额")
    private BigDecimal allowed_settle_amount;

    @Schema(description = "开票状态Undo,Doing,Done")
    private String invoice_status;

    @Schema(description = "付款状态,Undo,Doing,Done")
    private String settle_status;

    @Schema(description = "订单商品状态,")
    private OrderGoodsStatusEnum order_goods_status;
    @Schema(description = "退款商品状态")
    private OrderGoodsReturnsStatusEnum order_goods_returns_status;



    @Schema(description = "总金额(包含运费)")
    private BigDecimal amount;
    @Schema(description = "发票申请单编号")
    private String invoice_ids;
    @Schema(description = "结算申请单编号")
    private String settle_ids;

    @Schema(description = "服务费发票申请单编号")
    private String service_invoice_ids;
    @Schema(description = "已退款金额")
    private BigDecimal refund;
    @Schema(description = "备注")
    private String memo;

    @Schema(description = "扩展字段")
    private TradeExtendsAttr extends_attr;

    @Schema(description = "客户料号")
    private String sku;
    @Schema(description = "发票号码")
    private String invoice_number;
    @Schema(description = "平台组织ID")
    private SelectorFormat platform_organization_id;

    @Schema(description = "平台组织名称")
    private String platform_organization_name;
    @Schema(description = "客户付款方式")
    private String payment_type;
    @Schema(description = "订单收货人")
    private String order_receiver;
    @Schema(description = "订单收货地址")
    private String order_address;
    @Schema(description = "供应商(客户)名称,交易是Sale为供应商，采购则为客户")
    private String target_organization_name;

    @Schema(description = "未税单价")
    private BigDecimal pretax_price;
    @Schema(description = "商品未税金额（不包含运费）")
    private BigDecimal total_before_tax;
    @Schema(description = "总未税金额(包含运费)")
    private BigDecimal amount_before_tax;

    @Schema(description = "已申票数量")
    private BigDecimal already_invoice_quantity;
    @Schema(description = "已申票金额")
    private BigDecimal already_invoice_total;
    @Schema(description = "已开票金额")
    private BigDecimal invoiced;
    @Schema(description = "已收款金额")
    private BigDecimal amountreceived;
    @Schema(description = "单位")
    private String unit;
    @Schema(description = "交易创建时间")
    private String createdTime;


    @Schema(description = "ERP_PO行号")
    private String erp_po_line_number;
    @Schema(description = "外部工厂代码(拓展字段已含)")
    private String clientFactoryCode;

    //冗余字段
    @Schema(description = "品名")
    private String name;
    @Schema(description = "品牌-规格")
    private String model_spec;
    @Schema(description = "反向交易结算类型")
    private SettleType target_payment_type;
    @Schema(description = "反向交易结算状态")
    private TradeSettleStatus target_settle_statuss;
    @Schema(description = "是否小规模")
    private Boolean small_scale;


    public Map<String, Object> beanToMap(){
        Map<String, Object> param = BeanUtil.beanToMap(this);
        param.put("summary", summary == null ? "":JSON.toJSONString(this.getSummary()));
        param.put("extends_attr", extends_attr == null ? "":JSON.toJSONString(this.getExtends_attr()));
        param.put("TradeExtendsAttr", JSON.toJSONString(this.getExtends_attr()));
        param.put("target_payment_type", target_payment_type == null ? null:this.getTarget_payment_type().name());
        param.put("target_settle_statuss", target_settle_statuss == null ? null:this.getTarget_settle_statuss().name());
        param.put("order_goods_status", order_goods_status == null ? null:this.getOrder_goods_status().name());
        return param;
    }


    public void setSummary(String summary){
        this.summary = Summary.conversion(summary);
    }
    public void setExtends_attr(String extends_attr){
        this.extends_attr = TradeExtendsAttr.conversion(extends_attr);
    }


}
